package com.monitor.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.abel533.echarts.Option;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.Result;
import com.monitor.util.enums.PositonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/real")
@CrossOrigin
@Api(description="胎压监控")
public class JfzRealTimeController {
	@Autowired
	private JfzRealTimeService jfzRealTimeService;

	@Resource
	private ErpCustomerService erpCustomerService;

	@Resource
	private ErpCustomerCarService erpCustomerCarService;

	@Resource
    private ErpTpmsTireInformationService erpTpmsTireInformationService;

    @Resource
    private ErpTirePoliceService erpTirePoliceService;

//	@Resource	//jfz_newest
//	private  JfzRealTimeService jfzRealTimeService;


	@RequestMapping("/getList")
	public String getList(@RequestParam(value="page",defaultValue="1")int page,@RequestParam(value="limit",defaultValue="20")int limit,Model model){
		Map<String, Object> map=jfzRealTimeService.getList( page, limit);
		model.addAttribute("realMap", map);
		return "realList";
	}
	
	@RequestMapping("/getListByCar")
	@ApiOperation("获取所有车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页 默认1", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数 默认10", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public Map<String, Object> getListByCar(@RequestParam(value="page",defaultValue="1")int page,@RequestParam(value="limit",defaultValue="10")int limit){
		Map<String, Object> map=jfzRealTimeService.getListByCar( page, limit);
		return map;
	}
	
	
	@RequestMapping("/getListByCustomerId")
	@ApiOperation("通过用户获取所有车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "keyWord", value = "关键字(车牌号、自编车号、客户名称)", required = false, dataType = "String", paramType = "query")
    })
	@ResponseBody
	public Map<String, Object> getListByCustomerId(int custId,String keyWord){
		Map<String, Object> map=jfzRealTimeService.getListByCustomerId(custId,keyWord);
		return map;
	}

	
	@RequestMapping("/getListByErrorCar")
	@ApiOperation("获取所有报警车辆信息")
	@ResponseBody
	public Map<String, Object> getListByErrorCar(){
		Map<String, Object> map=jfzRealTimeService.getListByErrorCar();
		return map;
	}
	
	

	//获取折线图 type=1为温度 2为压力 3为速度
	@RequestMapping("/getEcharts")
	@ApiOperation("获取折线图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1为温度 2为压力 3为速度", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "carId", value = "汽车编号", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public String getEcharts(@RequestParam(value="startTime",defaultValue="")String startTime,@RequestParam(value="endTime",defaultValue="")String endTime,int type,int carId,String po) {
		Option op=jfzRealTimeService.getEcharts(startTime, endTime, type,carId);
		String json=JSON.toJSONString(op);
		return json;
	}



	@RequestMapping(value = "/getEchartsBing")
	@ApiOperation("获取报警管理-饼状图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1是报警原因 2是报警类型 3为胎位", required = false, dataType = "int", paramType = "query"),
    })
	@ResponseBody
	public String getEchartBing(@RequestParam(value="startTime",defaultValue="2000-01-01")String startTime,@RequestParam(value="endTime",defaultValue="2099-99-99")String endTime,int type) {
        Option echarBing = jfzRealTimeService.getEcharBing(startTime, endTime, type);
        String s = JSON.toJSONStringWithDateFormat(echarBing,"yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);

//		String json=JSON.toJSONString(op);
//		JSON.toJSONString( json, SerializerFeature.DisableCircularReferenceDetect); //出了乱码,.
////	String json=	JSONArray.toJSONString(op, SerializerFeature.DisableCircularReferenceDetect);
//        JSONObject.parse(JSONArray.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect));
        String json=JSON.toJSONString(echarBing);
        return s;
	}
	@RequestMapping(value = "/getEchartsZhe")
	@ApiOperation("获取报警管理-折线图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1是报警原因 2是报警类型 3为胎位", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public String getEchartZhe(@RequestParam(value="startTime",defaultValue="2000-01-01")String startTime,@RequestParam(value="endTime",defaultValue="2099-99-99")String endTime,int type) {
        Option echarBing = jfzRealTimeService.getEcharZhe(startTime, endTime, type);
        String s = JSON.toJSONStringWithDateFormat(echarBing,"yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);

        return s;
	}	@RequestMapping(value = "/getEchartsZhu")
	@ApiOperation("获取报警管理-柱状图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 默认不传", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "1是报警原因 2是报警类型 3为胎位", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public String getEchartZhu(@RequestParam(value="startTime",defaultValue="2000-01-01")String startTime,@RequestParam(value="endTime",defaultValue="2099-99-99")String endTime,int type) {
        Option echarBing = jfzRealTimeService.getEcharZhu(startTime, endTime, type);
        String s = JSON.toJSONStringWithDateFormat(echarBing,"yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);

        return s;
	}
	
	@RequestMapping("/selectTireArchives")
	@ApiOperation("获取车辆轮胎信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "po", value = "胎位", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "carId", value = "汽车编号", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public Map<String, Object> selectTireArchives(String po,int carId){
		ErpCustomerCar car=erpCustomerCarService.selectById(carId);
		if(car!=null) {
			if(car.getCarType3()!=null&&car.getCarType3()!=0) {
				po=PositonUtil.getStringByTypeId(po,car.getCarType3());
			}else {
				return null;
			}
		}else {
			return null;
		}
		return jfzRealTimeService.selectTireArchives(po, carId);
	}
	
	@RequestMapping("/selectCarTyre")
	@ApiOperation("根据车辆id获取胎位胎压温度和电压")
	@ApiImplicitParam(name = "carId", value = "carId", required = false, dataType = "int", paramType = "query")
	@ResponseBody
	public Map<String, Object> selectCarTyre(int carId){
 		return jfzRealTimeService.selectCarTyre(carId);
	}
	
	@RequestMapping("/selectByCustomeId")
	@ApiOperation("获取所有客户车辆信息")
	@ApiImplicitParam(name = "cId", value = "客户编号", required = false, dataType = "int", paramType = "query")
	@ResponseBody
	public Result<List<Map<String, Object>>> selectByCustomeId(Integer cId){
		return jfzRealTimeService.selectByCustomeId(cId);
	}
	
	@RequestMapping("/selectErrorList")
	@ApiOperation("获取报警管理列表")
    @ApiImplicitParams({
    	 @ApiImplicitParam(name = "startTime", value = "开始时间 默认不传查本月", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "endTime", value = "结束时间 默认不传查本月", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "cId", value = "客户编号", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "page", value = "当前页 默认1", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "limit", value = "每页条数 默认10", required = false, dataType = "int", paramType = "query")
    })
	@ResponseBody
	public Result<List<Map<String, Object>>> selectErrorList(@RequestParam(value="startTime",defaultValue="")String startTime,@RequestParam(value="endTime",defaultValue="")String endTime,Integer cId,@RequestParam(value="page",defaultValue="1")Integer page,@RequestParam(value="limit",defaultValue="10")Integer limit){
		return jfzRealTimeService.selectErrorList(startTime, endTime, cId, page, limit);
	}



	@RequestMapping("/selectFault")
	@ApiOperation("胎压监控-故障统计")
    @ApiImplicitParams({

         @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),
         @ApiImplicitParam(name = "startDate", value = "起始时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query")
    })
	@ResponseBody
	public Result<List<Integer>> selectFault(int custId,@RequestParam(value = "startDate", defaultValue = "2000-01-01")String startDate,@RequestParam(value = "endDate", defaultValue = "2099-01-01")String endDate){  //@RequestParam(value="startTime",defaultValue="")
        Result<List<Integer>> integerResult=new Result<>();
        try {


        List<ErpCustomerCar> customer_id = erpCustomerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("customer_id", custId));


        List <JfzRealTime>  jfzRealTimes=new ArrayList<JfzRealTime>();
        for (int i = 0; i < customer_id.size(); i++) {
            ErpCustomerCar erpCustomerCar =  customer_id.get(i);
            JfzRealTime     jfzRealTime =new JfzRealTime();
            jfzRealTime.setCarId(erpCustomerCar.getId());
            JfzRealTime jfzRealTime1 = jfzRealTimeService.selectById(jfzRealTime);
            jfzRealTimes.add(jfzRealTime1);
        }
				List<Integer> integers =new ArrayList<>();
        ErpTpmsTireInformation erpTpmsTireInformation = erpTpmsTireInformationService.selectById(10);
        integers.add(jfzRealTimeService.selectCount(new EntityWrapper<JfzRealTime>().lt("tire_pressure", erpTpmsTireInformation.getPressureSmall() * 0.9).between("recieve_time",startDate,endDate)));
        integers.add(  jfzRealTimeService.selectCount(new EntityWrapper<JfzRealTime>().gt("tire_pressure", erpTpmsTireInformation.getVoltageSmall() * 1.1).between("recieve_time",startDate,endDate)));
            integers.add(0);
            integerResult.setData(integers);
        }catch (Exception e){
            e.printStackTrace();
        }

        return integerResult;
	}


	@RequestMapping("/selectBaoPolice")
	@ApiOperation("胎压监控-报警管理")
    @ApiImplicitParams({

         @ApiImplicitParam(name = "startDate", value = "起始时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),
         @ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query"),

    })
	@ResponseBody
	public Result<Map<String,Object>>  selectBaoPolice(int custId,@RequestParam(value = "startDate", defaultValue = "2000-01-01")String startDate,@RequestParam(value = "endDate", defaultValue = "2099-01-01")String endDate){  //@RequestParam(value="startTime",defaultValue="")
        Result<Map<String,Object>>  mapResult=new  Result<Map<String,Object>> ();
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            ErpCustomer erpCustomer = erpCustomerService.selectById(custId);
            map.put("cust",erpCustomer);
            List<ErpCustomerCar> customer_id = erpCustomerCarService.selectList(new EntityWrapper<ErpCustomerCar>().eq("customer_id", custId));
//            map.put("car",customer_id);
            ErpTirePolice erpTirePolice=new ErpTirePolice();
            List<ErpTirePolice> erpTirePoliceList =new ArrayList<>();
            List<ErpCustomerCar> erpCustomerCars =new ArrayList<>();
            for (int i = 0; i < customer_id.size(); i++) {
                ErpCustomerCar erpCustomerCar =  customer_id.get(i);
                erpTirePolice.setCarId( erpCustomerCar.getId());

                ErpTirePolice tirePolice = erpTirePoliceService.selectOne(new EntityWrapper<ErpTirePolice>().eq("car_id",erpCustomerCar.getId()).between("police_dt",startDate,endDate));
               if (tirePolice!=null) {
               	switch (tirePolice.getPoliceType()){
					case  1:
						tirePolice.setPoliceTypeName("检测状态正常");
					case  2:
						tirePolice.setPoliceTypeName("低压/高压/高温报警");

					case  3:
						tirePolice.setPoliceTypeName("电池/花纹深度报警");

					case  4:
						tirePolice.setPoliceTypeName("传感器无信号报警");

					case  5:
						tirePolice.setPoliceTypeName("未安装传感器");
				}

                   ErpCustomerCar erpCustomerCar1 = erpCustomerCarService.selectOne(new EntityWrapper<ErpCustomerCar>().eq("id", tirePolice.getCarId()));
                   erpCustomerCars.add(erpCustomerCar1);

                   erpTirePoliceList.add(tirePolice);

               }
            }

            map.put("car",erpCustomerCars);
            map.put("erpTirePolice",erpTirePoliceList);

            mapResult.setData(map);
            mapResult.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            mapResult.setMsg("查询失败");
        }

        return mapResult;
	}
//	@RequestMapping("/selectReason")
//	@ApiOperation("压监控-报警管理, 报警原因")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "String", paramType = "query"),
//			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query")
//	})
//	@ResponseBody
//	public Result<Map<String, Object>> selectReason(@RequestParam(value = "startTime", defaultValue = "2000-01-01")String startTime, @RequestParam(value = "endTime", defaultValue = "2099-09-09")String endTime){
//        Result<Map<String, Object>> result=new   Result<Map<String, Object>>();
//        List<Map<String, Object>> stringStringMap = jfzRealTimeService.selectReason(startTime, endTime);
//        Map<String, Object> map=new HashMap<>();
//        List<Integer> count=new ArrayList<>();
//        List<String> reason=new ArrayList<>();
//        for (int i = 0; i < stringStringMap.size(); i++) {
//            Map<String, Object> stringObjectMap =  stringStringMap.get(i);
//            reason.add(String.valueOf(stringObjectMap.get("reason")));
//            count.add(Integer.valueOf(stringObjectMap.get("COUNT(1)").toString()));
//        }
//
//        map.put("count",count);
//         map.put("reason",reason);
//        result.setData(map);
//        return result;
//	}
//
//
//
//@RequestMapping("/selectLocation")
//	@ApiOperation("压监控-报警管理, 胎位")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "String", paramType = "query"),
//			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query")
//	})
//	@ResponseBody
//	public Result<Map<String, Object>> selectLocation(@RequestParam(value = "startTime", defaultValue = "2000-01-01")String startTime, @RequestParam(value = "endTime", defaultValue = "2099-09-09")String endTime){
//        Result<Map<String, Object>> result=new   Result<Map<String, Object>>();
//        List<Map<String, Object>> stringStringMap = jfzRealTimeService.selectLocation(startTime, endTime);
//        Map<String, Object> map=new HashMap<>();
//        List<Integer> count=new ArrayList<>();
//        List<String> location=new ArrayList<>();
//        for (int i = 0; i < stringStringMap.size(); i++) {
//            Map<String, Object> stringObjectMap =  stringStringMap.get(i);
//            location.add(String.valueOf(stringObjectMap.get("tire_location")));
//            count.add(Integer.valueOf(stringObjectMap.get("COUNT(1)").toString()));
//        }
//
//        map.put("count",count);
//         map.put("reason",location);
//        result.setData(map);
//        return result;
//	}
//	@RequestMapping("/selectType")
//	@ApiOperation("压监控-报警管理, 报警类型")
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "startTime", value = "起始时间", required = false, dataType = "String", paramType = "query"),
//			@ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query")
//	})
//	@ResponseBody
//	public Result<Map<String, Object>> selectType(@RequestParam(value = "startTime", defaultValue = "2000-01-01")String startTime,@RequestParam(value = "endTime", defaultValue = "2099-09-09") String endTime){
//        Result<Map<String, Object>> result=new   Result<Map<String, Object>>();
//        List<Map<String, Object>> stringStringMap = jfzRealTimeService.selectType(startTime, endTime);
//        Map<String, Object> map=new HashMap<>();
//        List<Integer> count=new ArrayList<>();
//        List<String> Type=new ArrayList<>();
//        for (int i = 0; i < stringStringMap.size(); i++) {
//            Map<String, Object> stringObjectMap =  stringStringMap.get(i);
//            Type.add(String.valueOf(stringObjectMap.get("police_type")));
//            count.add(Integer.valueOf(stringObjectMap.get("COUNT(1)").toString()));
//        }
//
//
//        map.put("count",count);
//         map.put("reason",Type);
//        result.setData(map);
//        return result;
//	}


	@RequestMapping("/statistics")
	@ResponseBody
	@ApiOperation("胎压监控-统计分析")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "起始时间", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "code", value = "客户编码", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "page", value = "当前页 默认1", required = false, dataType = "int", paramType = "query"),
	        @ApiImplicitParam(name = "limit", value = "每页条数 默认10", required = false, dataType = "int", paramType = "query")
	})
	public Result<List<StatisticalAnalysis>> statistics(String startTime,String endTime,String code,@RequestParam(value="page",defaultValue="1")Integer page,@RequestParam(value="limit",defaultValue="10")Integer limit){
		return erpTirePoliceService.selectTong(startTime, endTime, code, page, limit);
	}
}
